<?php

namespace App\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

class ShopPointsReceiveLog extends Model
{
    use HasFactory;
    protected $table = "shop_points_receive_list";
    protected $guarded = [];
    public static function receive($data){
        $activity_id=$data['activity_id']??null;
        $room_id=$data['room_id']??null;
        $shop_id=$data['shop_id']??null;
        $host_id=$data['host_id']??null;
        $room_name=$data['room_name']??null;
        $shop_name=$data['shop_name']??null;
        $host_name=$data['host_name']??null;
        $receive_id=$data['receive_id']??null;
        $receive_name=$data['receive_name']??null;
        $points=$data['points']??null;
        $point=$data['point']??null;
        if(empty($activity_id)||empty($room_id)||empty($shop_id)||empty($host_id)||empty($receive_id)||empty($point)){
            return null;
        }
        $shop=Redis::get(Shop::REDIS_PREFIX.$shop_id);
        $host=Redis::get(ShopHost::REDIS_PREFIX.$host_id);
        if(empty($shop)){
            return null;
        }
        if(empty($host)){
            return null;
        }
        return self::create([
            'activity_id'=>$activity_id,
            'room_id'=>$room_id,
            'shop_id'=>$shop_id,
            'host_id'=>$host_id,
            'room_id'=>$room_id,
            'room_name'=>$room_name,
            'host_name'=>$host_name,
            'receive_id'=>$receive_id,
            'receive_name'=>$receive_name,
            'points'=>$points,
            'point'=>$point
        ]);
    }
    public function sendLog(){
        return $this->hasOne(ShopPointsSendLog::class,'room_id','room_id')->orderBy('id','desc');
    }
    public function activity(){
        return $this->hasOne(ShopPointsSendLog::class,'id','activity_id');
    }
    protected function serializeDate(DateTimeInterface $date){
        return $date->format('Y-m-d H:i:s');
    }
    public static function copy2_ShopSystemReceiveLog(){
        
    }
    public static function copy2_ShopUserReceiveLog(){
        
    }
}
